package o;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.media.NotProvisionedException;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Pair;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.drm.DrmInitData.SchemeData;
import com.google.android.exoplayer2.drm.DrmSession;
import com.google.android.exoplayer2.drm.DrmSession.DrmSessionException;
import com.google.android.exoplayer2.drm.KeysExpiredException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import o.adk.a;

@TargetApi(18)
public class adc<T extends ado> implements DrmSession<T> {
    private final int ʻ;
    private o.adk.e ʻॱ;
    private final amu<adj> ʼ;
    private final e<T> ʽ;
    final c ˊ;
    private d ˊॱ;
    private final adk<T> ˋ;
    private int ˋॱ;
    final UUID ˎ;
    final adl ˏ;
    private int ˏॱ;
    private HandlerThread ͺ;
    @Nullable
    public final List<SchemeData> ॱ;
    private T ॱˊ;
    private byte[] ॱˋ;
    private a ॱˎ;
    private final int ॱॱ;
    private DrmSessionException ॱᐝ;
    private final HashMap<String, String> ᐝ;
    @Nullable
    private byte[] ᐝॱ;

    public interface e<T extends ado> {
        void ˋ();

        void ˎ(adc<T> o_adc_T);

        void ˏ(Exception exception);
    }

    @SuppressLint({"HandlerLeak"})
    class c extends Handler {
        final /* synthetic */ adc ˏ;

        public c(adc o_adc, Looper looper) {
            this.ˏ = o_adc;
            super(looper);
        }

        public void handleMessage(Message message) {
            Pair pair = (Pair) message.obj;
            Object obj = pair.first;
            Object obj2 = pair.second;
            switch (message.what) {
                case 0:
                    this.ˏ.ˏ(obj, obj2);
                    return;
                case 1:
                    this.ˏ.ˊ(obj, obj2);
                    return;
                default:
                    return;
            }
        }
    }

    @SuppressLint({"HandlerLeak"})
    class d extends Handler {
        final /* synthetic */ adc ˋ;

        public d(adc o_adc, Looper looper) {
            this.ˋ = o_adc;
            super(looper);
        }

        void ˎ(int i, Object obj, boolean z) {
            int i2;
            if (z) {
                i2 = 1;
            } else {
                i2 = 0;
            }
            obtainMessage(i, i2, 0, obj).sendToTarget();
        }

        /* JADX WARNING: inconsistent code. */
        /* Code decompiled incorrectly, please refer to instructions dump. */
        public void handleMessage(android.os.Message r6) {
            /*
            r5 = this;
            r2 = r6.obj;
            r1 = r6.what;	 Catch:{ Exception -> 0x000d }
            switch(r1) {
                case 0: goto L_0x0015;
                case 1: goto L_0x0037;
                default: goto L_0x0007;
            };	 Catch:{ Exception -> 0x000d }
        L_0x0007:
            r1 = new java.lang.RuntimeException;	 Catch:{ Exception -> 0x000d }
            r1.<init>();	 Catch:{ Exception -> 0x000d }
            throw r1;	 Catch:{ Exception -> 0x000d }
        L_0x000d:
            r1 = move-exception;
            r3 = r5.ˋ(r6);
            if (r3 == 0) goto L_0x0025;
        L_0x0014:
            return;
        L_0x0015:
            r1 = r5.ˋ;	 Catch:{ Exception -> 0x000d }
            r3 = r1.ˏ;	 Catch:{ Exception -> 0x000d }
            r1 = r5.ˋ;	 Catch:{ Exception -> 0x000d }
            r4 = r1.ˎ;	 Catch:{ Exception -> 0x000d }
            r0 = r2;
            r0 = (o.adk.a) r0;	 Catch:{ Exception -> 0x000d }
            r1 = r0;
            r1 = r3.ˎ(r4, r1);	 Catch:{ Exception -> 0x000d }
        L_0x0025:
            r3 = r5.ˋ;
            r3 = r3.ˊ;
            r4 = r6.what;
            r1 = android.util.Pair.create(r2, r1);
            r1 = r3.obtainMessage(r4, r1);
            r1.sendToTarget();
            goto L_0x0014;
        L_0x0037:
            r1 = r5.ˋ;	 Catch:{ Exception -> 0x000d }
            r3 = r1.ˏ;	 Catch:{ Exception -> 0x000d }
            r1 = r5.ˋ;	 Catch:{ Exception -> 0x000d }
            r4 = r1.ˎ;	 Catch:{ Exception -> 0x000d }
            r0 = r2;
            r0 = (o.adk.e) r0;	 Catch:{ Exception -> 0x000d }
            r1 = r0;
            r1 = r3.ˊ(r4, r1);	 Catch:{ Exception -> 0x000d }
            goto L_0x0025;
            */
            throw new UnsupportedOperationException("Method not decompiled: o.adc.d.handleMessage(android.os.Message):void");
        }

        private boolean ˋ(Message message) {
            if (!(message.arg1 == 1)) {
                return false;
            }
            int i = message.arg2 + 1;
            if (i > this.ˋ.ॱॱ) {
                return false;
            }
            Message obtain = Message.obtain(message);
            obtain.arg2 = i;
            sendMessageDelayed(obtain, ˎ(i));
            return true;
        }

        private long ˎ(int i) {
            return (long) Math.min((i - 1) * 1000, 5000);
        }
    }

    public adc(UUID uuid, adk<T> o_adk_T, e<T> eVar, @Nullable List<SchemeData> list, int i, @Nullable byte[] bArr, HashMap<String, String> hashMap, adl o_adl, Looper looper, amu<adj> o_amu_o_adj, int i2) {
        this.ˎ = uuid;
        this.ʽ = eVar;
        this.ˋ = o_adk_T;
        this.ʻ = i;
        this.ᐝॱ = bArr;
        this.ॱ = bArr == null ? Collections.unmodifiableList(list) : null;
        this.ᐝ = hashMap;
        this.ˏ = o_adl;
        this.ॱॱ = i2;
        this.ʼ = o_amu_o_adj;
        this.ˏॱ = 2;
        this.ˊ = new c(this, looper);
        this.ͺ = new HandlerThread("DrmRequestHandler");
        this.ͺ.start();
        this.ˊॱ = new d(this, this.ͺ.getLooper());
    }

    public void ॱ() {
        int i = this.ˋॱ + 1;
        this.ˋॱ = i;
        if (i == 1 && this.ˏॱ != 1 && ˊ(true)) {
            ˋ(true);
        }
    }

    public boolean ˏ() {
        int i = this.ˋॱ - 1;
        this.ˋॱ = i;
        if (i != 0) {
            return false;
        }
        this.ˏॱ = 0;
        this.ˊ.removeCallbacksAndMessages(null);
        this.ˊॱ.removeCallbacksAndMessages(null);
        this.ˊॱ = null;
        this.ͺ.quit();
        this.ͺ = null;
        this.ॱˊ = null;
        this.ॱᐝ = null;
        this.ʻॱ = null;
        this.ॱˎ = null;
        if (this.ॱˋ != null) {
            this.ˋ.ˏ(this.ॱˋ);
            this.ॱˋ = null;
            this.ʼ.ˊ(ada.ˋ);
        }
        return true;
    }

    public boolean ˋ(byte[] bArr) {
        return Arrays.equals(this.ॱˋ, bArr);
    }

    public void ˏ(int i) {
        if (ॱˊ()) {
            switch (i) {
                case 1:
                    this.ˏॱ = 3;
                    this.ʽ.ˎ(this);
                    return;
                case 2:
                    ˋ(false);
                    return;
                case 3:
                    ͺ();
                    return;
                default:
                    return;
            }
        }
    }

    public void ˊ() {
        this.ॱˎ = this.ˋ.ˊ();
        this.ˊॱ.ˎ(0, this.ॱˎ, true);
    }

    public void ˎ() {
        if (ˊ(false)) {
            ˋ(true);
        }
    }

    public void ˋ(Exception exception) {
        ˊ(exception);
    }

    public final int ˋ() {
        return this.ˏॱ;
    }

    public final DrmSessionException ॱॱ() {
        return this.ˏॱ == 1 ? this.ॱᐝ : null;
    }

    public final T ʼ() {
        return this.ॱˊ;
    }

    public Map<String, String> ʻ() {
        return this.ॱˋ == null ? null : this.ˋ.ˋ(this.ॱˋ);
    }

    private boolean ˊ(boolean z) {
        if (ॱˊ()) {
            return true;
        }
        try {
            this.ॱˋ = this.ˋ.ˎ();
            this.ʼ.ˊ(adb.ˊ);
            this.ॱˊ = this.ˋ.ॱ(this.ॱˋ);
            this.ˏॱ = 3;
            return true;
        } catch (Exception e) {
            if (z) {
                this.ʽ.ˎ(this);
            } else {
                ˊ(e);
            }
            return false;
        } catch (Exception e2) {
            ˊ(e2);
            return false;
        }
    }

    private void ˏ(Object obj, Object obj2) {
        if (obj != this.ॱˎ) {
            return;
        }
        if (this.ˏॱ == 2 || ॱˊ()) {
            this.ॱˎ = null;
            if (obj2 instanceof Exception) {
                this.ʽ.ˏ((Exception) obj2);
                return;
            }
            try {
                this.ˋ.ˊ((byte[]) obj2);
                this.ʽ.ˋ();
            } catch (Exception e) {
                this.ʽ.ˏ(e);
            }
        }
    }

    private void ˋ(boolean z) {
        switch (this.ʻ) {
            case 0:
            case 1:
                if (this.ᐝॱ == null) {
                    ˋ(1, z);
                    return;
                } else if (this.ˏॱ == 4 || ʽ()) {
                    long ᐝ = ᐝ();
                    if (this.ʻ == 0 && ᐝ <= 60) {
                        amz.ˊ("DefaultDrmSession", "Offline license has expired or will expire soon. Remaining seconds: " + ᐝ);
                        ˋ(2, z);
                        return;
                    } else if (ᐝ <= 0) {
                        ˊ(new KeysExpiredException());
                        return;
                    } else {
                        this.ˏॱ = 4;
                        this.ʼ.ˊ(ade.ˊ);
                        return;
                    }
                } else {
                    return;
                }
            case 2:
                if (this.ᐝॱ == null) {
                    ˋ(2, z);
                    return;
                } else if (ʽ()) {
                    ˋ(2, z);
                    return;
                } else {
                    return;
                }
            case 3:
                if (ʽ()) {
                    ˋ(3, z);
                    return;
                }
                return;
            default:
                return;
        }
    }

    private boolean ʽ() {
        try {
            this.ˋ.ˏ(this.ॱˋ, this.ᐝॱ);
            return true;
        } catch (Exception e) {
            amz.ˊ("DefaultDrmSession", "Error trying to restore Widevine keys.", e);
            ˊ(e);
            return false;
        }
    }

    private long ᐝ() {
        if (!C.ˊ.equals(this.ˎ)) {
            return Long.MAX_VALUE;
        }
        Pair ˊ = ads.ˊ(this);
        return Math.min(((Long) ˊ.first).longValue(), ((Long) ˊ.second).longValue());
    }

    private void ˋ(int i, boolean z) {
        try {
            this.ʻॱ = this.ˋ.ˏ(i == 3 ? this.ᐝॱ : this.ॱˋ, this.ॱ, i, this.ᐝ);
            this.ˊॱ.ˎ(1, this.ʻॱ, z);
        } catch (Exception e) {
            ˎ(e);
        }
    }

    private void ˊ(Object obj, Object obj2) {
        if (obj == this.ʻॱ && ॱˊ()) {
            this.ʻॱ = null;
            if (obj2 instanceof Exception) {
                ˎ((Exception) obj2);
                return;
            }
            try {
                byte[] bArr = (byte[]) obj2;
                if (this.ʻ == 3) {
                    this.ˋ.ˎ(this.ᐝॱ, bArr);
                    this.ʼ.ˊ(ade.ˊ);
                    return;
                }
                byte[] ˎ = this.ˋ.ˎ(this.ॱˋ, bArr);
                if (!((this.ʻ != 2 && (this.ʻ != 0 || this.ᐝॱ == null)) || ˎ == null || ˎ.length == 0)) {
                    this.ᐝॱ = ˎ;
                }
                this.ˏॱ = 4;
                this.ʼ.ˊ(adg.ˏ);
            } catch (Exception e) {
                ˎ(e);
            }
        }
    }

    private void ͺ() {
        if (this.ˏॱ == 4) {
            this.ˏॱ = 3;
            ˊ(new KeysExpiredException());
        }
    }

    private void ˎ(Exception exception) {
        if (exception instanceof NotProvisionedException) {
            this.ʽ.ˎ(this);
        } else {
            ˊ(exception);
        }
    }

    private void ˊ(Exception exception) {
        this.ॱᐝ = new DrmSessionException(exception);
        this.ʼ.ˊ(new adi(exception));
        if (this.ˏॱ != 4) {
            this.ˏॱ = 1;
        }
    }

    private boolean ॱˊ() {
        return this.ˏॱ == 3 || this.ˏॱ == 4;
    }
}
